﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CNRegex.Biz.BizAccess;
using CNRegex.Web.Base;
using CNRegex.Entity;
using CNRegex.Entity.EntityEx;
using CNRegex.Web.Utility;
using Project.Common;
using System.Web.Mvc;
using CNRegex.Enum;
using CNRegex.Entity.Others;
using CNRegex.Biz;
using CNRegex.Biz.SPBiz;

namespace CNRegex.Web.Models
{
	public class ModelRegexHome : BaseModel
	{
		private BizRegexCategory bizRegexCategory;
		private BizRegexExpression bizRegexExpression;
		private BizRegexFavourite bizRegexFavourite;
		private static IList<RegexFavourite> emptyRegexFavourite = new List<RegexFavourite>();
		
		public string RegexCategoryName { get; set; }
		public int? PageIndex { get; set; }
		public ModelRegexHome()
		{
		}

		private PaginateHelper paginate;
		public PaginateHelper Paginate
		{
			get {
				expressiones = Expressiones;
				return paginate; 
			}
		}

		private IList<RegexFavourite> regexFavourites;
		public IList<RegexFavourite> RegexFavourites
		{
			get
			{
				if (regexFavourites != null)
					return regexFavourites;
				expressiones = Expressiones;
				return regexFavourites;
			}
		}

		private IList<RegexExpressionEx> expressiones;
		public IList<RegexExpressionEx> Expressiones
		{
			get
			{
				if (expressiones != null)
					return expressiones;
				Guid selectCategoryID;
				if (string.IsNullOrEmpty(RegexCategoryName))
				{
					selectCategoryID = RegexCategories[0].ID;
					RegexCategoryName = RegexCategories[0].Name;
				}
				else
				{
					IList<RegexCategory> list = RegexCategories.Where(x => x.Name == RegexCategoryName).ToList();
					if (list.Count > 0)
						selectCategoryID = list[0].ID;
					else
						selectCategoryID = RegexCategories[0].ID;
				}
				int count = 0;
				expressiones = bizRegexExpression.ListByCategory(ProjectGlobal.PAGE_SIZE, PageIndex, selectCategoryID, ref count);
				paginate = new PaginateHelper(ProjectGlobal.PAGE_SIZE, count, PageIndex ?? 0, string.Format("/RegexHome/?id={0}&", RegexCategoryName));
				if (FrontContext.CurrentRegexUser != null)
					regexFavourites = bizRegexFavourite.ListBy(x => x.UserID == FrontContext.CurrentRegexUser.ID,"", EnumOrder.ASC);
				else
					regexFavourites = emptyRegexFavourite;
				return expressiones;
			}
		}

	}

	public class ModelRegexDetail : BaseModel
	{
		private BizRegexExpression bizRegexExpression;
		private BizRegexExpressionComment bizRegexExpressionComment;
		private BizRegexUser bizRegexUser;
		private string expressionID;
		public int? PageIndex { get; set; }
		public ModelRegexDetail(string expressionID)
		{
			bizRegexExpression = BizFactory.Get<BizRegexExpression>();
			bizRegexExpressionComment = BizFactory.Get<BizRegexExpressionComment>();
			bizRegexUser = BizFactory.Get<BizRegexUser>();
			this.expressionID = expressionID;

			//increase click count
			CurrentExpression.ClickCount = CurrentExpression.ClickCount == null ? 1 : CurrentExpression.ClickCount + 1;
			bizRegexExpression.Update(CurrentExpression);
		}

		private RegexExpression currentExpression;
		public RegexExpression CurrentExpression
		{
			get
			{
				if (currentExpression == null)
					currentExpression = bizRegexExpression.GetByID(new Guid(expressionID));
				return currentExpression;
			}
		}

		public RegexUser ExpressionCreator
		{
			get
			{
				if (currentExpression == null)
					currentExpression = CurrentExpression;
				return bizRegexUser.GetByID(CurrentExpression.UserID);
			}
		}

		private PaginateHelper paginate;
		public PaginateHelper Paginate
		{
			get
			{
				currentExpressionComments = CurrentExpressionComments;
				return paginate;
			}
		}

		private IList<RegexExpressionCommentEx> currentExpressionComments;
		public IList<RegexExpressionCommentEx> CurrentExpressionComments
		{
			get
			{
				if (currentExpressionComments != null)
					return currentExpressionComments;
				int count = 0;
				currentExpressionComments = bizRegexExpressionComment.List(ProjectGlobal.PAGE_SIZE, PageIndex, new Guid(expressionID), ref count);
				paginate = new PaginateHelper(ProjectGlobal.PAGE_SIZE, count, PageIndex ?? 0, string.Format("/RegexHome/Detail/?id={0}&", expressionID));
				paginate.EmptyTip = "暂无评论,简单讨论一下吧～～";
				return currentExpressionComments;
			}
		}

	}

	public class ModelRegexSearch : BaseModel
	{
		private BizRegexFavourite bizRegexFavourite;
		private BizRegexExpression bizRegexExpression;
		private static IList<RegexFavourite> emptyRegexFavourite = new List<RegexFavourite>();
		public ModelRegexSearch()
		{
			bizRegexExpression = BizFactory.Get<BizRegexExpression>();
			bizRegexFavourite = BizFactory.Get<BizRegexFavourite>();
		}

		public int? PageIndex { get; set; }

		private SearchExpressionEntity searchExpression;
		public SearchExpressionEntity SearchExpression
		{
			get
			{
				if (searchExpression != null)
				{
					if (searchExpression.OrderClick == null && searchExpression.OrderRecommend == null&&searchExpression.OrderCreateDate==null)
						searchExpression.OrderCreateDate = EnumOrder.DESC;
					return searchExpression;
				}
				return null;
			}
			set
			{
				searchExpression = value;
			}
		}

		private IList<SelectListItem> expressionCatalog;
		public IList<SelectListItem> ExpressionCatalog
		{
			get
			{
				if (expressionCatalog != null)
					return expressionCatalog;
				SelectList selectList = new SelectList(RegexCategories, "ID", "Name");
				expressionCatalog = WebTools.InsertOption(selectList, "", "", false, Enum.EnumLoaction.First);
				return expressionCatalog;
			}
		}

		private IList<SPSearchExpression> expressiones;
		public IList<SPSearchExpression> Expressiones
		{
			get
			{
				if (expressiones != null)
					return expressiones;
				expressiones = BizFactory.Get<BizSPSearchExpression>().Invoke(new SPSearchExpressionParameter() 
															{	
																catalogID=SearchExpression.UserExpressionCatalogID,
																keyWords=SearchExpression.Keywords,
																orderClick = WebTools.ConvertNullableEnumToInt<EnumOrder?, EnumOrder>(SearchExpression.OrderClick),
																orderCreateDate = WebTools.ConvertNullableEnumToInt<EnumOrder?, EnumOrder>(SearchExpression.OrderCreateDate),
																orderRecommend = WebTools.ConvertNullableEnumToInt<EnumOrder?, EnumOrder>(SearchExpression.OrderRecommend),
																pageIndex=  PageIndex ?? 0,
																pageSize=ProjectGlobal.PAGE_SIZE
															});
				string orderKey = "";
				string direction = "";
				if (SearchExpression.OrderClick != null)
				{
					orderKey = "click";
					direction = ((int)SearchExpression.OrderClick).ToString();
				}
				else if (SearchExpression.OrderRecommend != null)
				{
					orderKey = "recommend";
					direction = ((int)SearchExpression.OrderRecommend).ToString();
				}
				else
				{
					orderKey = "date";
					direction = ((int)SearchExpression.OrderCreateDate).ToString();
				}

				exPaginate = new PaginateHelper(ProjectGlobal.PAGE_SIZE, expressiones.Count > 0 ? expressiones[0].RecordCount.Value : 0, PageIndex ?? 0, string.Format("/RegexHome/Search/?c={0}&k={1}&ok={2}&d={3}&", SearchExpression.UserExpressionCatalogID, HttpUtility.UrlEncode(SearchExpression.Keywords),orderKey,direction));
				exPaginate.ParaPageIndex = "p";

				if (FrontContext.CurrentRegexUser != null)
					regexFavourites = bizRegexFavourite.ListBy(x => x.UserID == FrontContext.CurrentRegexUser.ID,"", EnumOrder.ASC);
				else
					regexFavourites = emptyRegexFavourite;

				return expressiones;
			}
		}

		private IList<RegexFavourite> regexFavourites;
		public IList<RegexFavourite> RegexFavourites
		{
			get
			{
				if (regexFavourites != null)
					return regexFavourites;
				expressiones = Expressiones;
				return regexFavourites;
			}
		}

		private PaginateHelper exPaginate;
		public PaginateHelper ExPaginate
		{
			get
			{
				if (exPaginate != null)
					return exPaginate;
				expressiones = Expressiones;
				return exPaginate;
			}
		}
	}
}